{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用pandas（Python Data Analysis的简写，意为Python数据分析）库加载这些数据，pandas在数据处理方面特别有用。  \n",
    "Python内置了读写CSV文件的csv库。但是，考虑到后面创建新特征时还要用到pandas更强大的一些函数，所以我们干脆用pandas加载数据文件。  \n",
    "pandas库是用来加载、管理和处理数据的。它在后台处理数据结构，支持诸计算均值等分析方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor/Neutral</th>\n",
       "      <th>PTS</th>\n",
       "      <th>Home/Neutral</th>\n",
       "      <th>PTS.1</th>\n",
       "      <th>Unnamed: 6</th>\n",
       "      <th>Unnamed: 7</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Tue Oct 25 2016</td>\n",
       "      <td>7:30 pm</td>\n",
       "      <td>New York Knicks</td>\n",
       "      <td>88</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>117</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tue Oct 25 2016</td>\n",
       "      <td>10:30 pm</td>\n",
       "      <td>San Antonio Spurs</td>\n",
       "      <td>129</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>100</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Tue Oct 25 2016</td>\n",
       "      <td>10:00 pm</td>\n",
       "      <td>Utah Jazz</td>\n",
       "      <td>104</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>113</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Wed Oct 26 2016</td>\n",
       "      <td>7:30 pm</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>117</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>122</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Wed Oct 26 2016</td>\n",
       "      <td>7:00 pm</td>\n",
       "      <td>Dallas Mavericks</td>\n",
       "      <td>121</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>130</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>OT</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Date Start (ET)    Visitor/Neutral  PTS            Home/Neutral  \\\n",
       "0  Tue Oct 25 2016    7:30 pm    New York Knicks   88     Cleveland Cavaliers   \n",
       "1  Tue Oct 25 2016   10:30 pm  San Antonio Spurs  129   Golden State Warriors   \n",
       "2  Tue Oct 25 2016   10:00 pm          Utah Jazz  104  Portland Trail Blazers   \n",
       "3  Wed Oct 26 2016    7:30 pm      Brooklyn Nets  117          Boston Celtics   \n",
       "4  Wed Oct 26 2016    7:00 pm   Dallas Mavericks  121          Indiana Pacers   \n",
       "\n",
       "   PTS.1 Unnamed: 6 Unnamed: 7 Notes  \n",
       "0    117  Box Score        NaN   NaN  \n",
       "1    100  Box Score        NaN   NaN  \n",
       "2    113  Box Score        NaN   NaN  \n",
       "3    122  Box Score        NaN   NaN  \n",
       "4    130  Box Score         OT   NaN  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "dataset = pd.read_csv(\"2016-17NBAResults.csv\") #加载数据 保存到数据框（dataframe）中\n",
    "dataset.iloc[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>start(ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>PTS</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>PTS.1</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>OT</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2016-10-25</td>\n",
       "      <td>7:30 pm</td>\n",
       "      <td>New York Knicks</td>\n",
       "      <td>88</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>117</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2016-10-25</td>\n",
       "      <td>10:30 pm</td>\n",
       "      <td>San Antonio Spurs</td>\n",
       "      <td>129</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>100</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016-10-25</td>\n",
       "      <td>10:00 pm</td>\n",
       "      <td>Utah Jazz</td>\n",
       "      <td>104</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>113</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2016-10-26</td>\n",
       "      <td>7:30 pm</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>117</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>122</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2016-10-26</td>\n",
       "      <td>7:00 pm</td>\n",
       "      <td>Dallas Mavericks</td>\n",
       "      <td>121</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>130</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>OT</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date start(ET)       Visitor Team  PTS               Home Team  PTS.1  \\\n",
       "0 2016-10-25   7:30 pm    New York Knicks   88     Cleveland Cavaliers    117   \n",
       "1 2016-10-25  10:30 pm  San Antonio Spurs  129   Golden State Warriors    100   \n",
       "2 2016-10-25  10:00 pm          Utah Jazz  104  Portland Trail Blazers    113   \n",
       "3 2016-10-26   7:30 pm      Brooklyn Nets  117          Boston Celtics    122   \n",
       "4 2016-10-26   7:00 pm   Dallas Mavericks  121          Indiana Pacers    130   \n",
       "\n",
       "  Score Type   OT Notes  \n",
       "0  Box Score  NaN   NaN  \n",
       "1  Box Score  NaN   NaN  \n",
       "2  Box Score  NaN   NaN  \n",
       "3  Box Score  NaN   NaN  \n",
       "4  Box Score   OT   NaN  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#pandas.read_csv函数提供了可用来修复数据的参数，导入文件时指定这几个参数就好。导入后，我们还可以修改文件的头部\n",
    "dataset = pd.read_csv(\"2016-17NBAResults.csv\",parse_dates=[\"Date\"])\n",
    "dataset.columns = [\"Date\",\"start(ET)\",\"Visitor Team\",\"PTS\",\"Home Team\",\"PTS.1\",\"Score Type\",\"OT\",\"Notes\"]\n",
    "dataset.iloc[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过组合和比较现有数据抽取特征。首先，确定类别值。在测试阶段，拿算法得到的分类结果与它对比，就能知道结果是否正确。  \n",
    "类别可以有多种表示方法，我们这里用1表示主场队获胜，用0表示客场队获胜。对于篮球比赛而言，得分最多的队伍获胜"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>start(ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>PTS</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>PTS.1</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>OT</th>\n",
       "      <th>Notes</th>\n",
       "      <th>HomeWin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2016-10-25</td>\n",
       "      <td>7:30 pm</td>\n",
       "      <td>New York Knicks</td>\n",
       "      <td>88</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>117</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2016-10-25</td>\n",
       "      <td>10:30 pm</td>\n",
       "      <td>San Antonio Spurs</td>\n",
       "      <td>129</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>100</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016-10-25</td>\n",
       "      <td>10:00 pm</td>\n",
       "      <td>Utah Jazz</td>\n",
       "      <td>104</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>113</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2016-10-26</td>\n",
       "      <td>7:30 pm</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>117</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>122</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2016-10-26</td>\n",
       "      <td>7:00 pm</td>\n",
       "      <td>Dallas Mavericks</td>\n",
       "      <td>121</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>130</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>OT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date start(ET)       Visitor Team  PTS               Home Team  PTS.1  \\\n",
       "0 2016-10-25   7:30 pm    New York Knicks   88     Cleveland Cavaliers    117   \n",
       "1 2016-10-25  10:30 pm  San Antonio Spurs  129   Golden State Warriors    100   \n",
       "2 2016-10-25  10:00 pm          Utah Jazz  104  Portland Trail Blazers    113   \n",
       "3 2016-10-26   7:30 pm      Brooklyn Nets  117          Boston Celtics    122   \n",
       "4 2016-10-26   7:00 pm   Dallas Mavericks  121          Indiana Pacers    130   \n",
       "\n",
       "  Score Type   OT Notes  HomeWin  \n",
       "0  Box Score  NaN   NaN     True  \n",
       "1  Box Score  NaN   NaN    False  \n",
       "2  Box Score  NaN   NaN     True  \n",
       "3  Box Score  NaN   NaN     True  \n",
       "4  Box Score   OT   NaN     True  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 找出主场获胜的球队：\n",
    "dataset[\"HomeWin\"] = dataset[\"PTS\"] < dataset[\"PTS.1\"]\n",
    "y_true = dataset[\"HomeWin\"].values # y_true数组保存的是类别数据\n",
    "dataset.iloc[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先，创建两个能帮助我们进行预测的特征，分别是这两支队伍上场比赛的胜负情况。赢得上场比赛，大致可以说明该球队水平较高。\n",
    "遍历每一行数据，记录获胜球队。当到达一行新数据时，分别查看该行数据中的两支球队在各自的上一场比赛中有没有获胜的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>start(ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>PTS</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>PTS.1</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>OT</th>\n",
       "      <th>Notes</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2016-10-28</td>\n",
       "      <td>8:00 pm</td>\n",
       "      <td>Charlotte Hornets</td>\n",
       "      <td>97</td>\n",
       "      <td>Miami Heat</td>\n",
       "      <td>91</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2016-10-28</td>\n",
       "      <td>9:30 pm</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>122</td>\n",
       "      <td>New Orleans Pelicans</td>\n",
       "      <td>114</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2016-10-28</td>\n",
       "      <td>8:00 pm</td>\n",
       "      <td>Phoenix Suns</td>\n",
       "      <td>110</td>\n",
       "      <td>Oklahoma City Thunder</td>\n",
       "      <td>113</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>OT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2016-10-28</td>\n",
       "      <td>7:00 pm</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>94</td>\n",
       "      <td>Toronto Raptors</td>\n",
       "      <td>91</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>2016-10-28</td>\n",
       "      <td>9:00 pm</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>89</td>\n",
       "      <td>Utah Jazz</td>\n",
       "      <td>96</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date start(ET)           Visitor Team  PTS              Home Team  \\\n",
       "20 2016-10-28   8:00 pm      Charlotte Hornets   97             Miami Heat   \n",
       "21 2016-10-28   9:30 pm  Golden State Warriors  122   New Orleans Pelicans   \n",
       "22 2016-10-28   8:00 pm           Phoenix Suns  110  Oklahoma City Thunder   \n",
       "23 2016-10-28   7:00 pm    Cleveland Cavaliers   94        Toronto Raptors   \n",
       "24 2016-10-28   9:00 pm     Los Angeles Lakers   89              Utah Jazz   \n",
       "\n",
       "    PTS.1 Score Type   OT Notes  HomeWin  HomeLastWin  VisitorLastWin  \n",
       "20     91  Box Score  NaN   NaN    False         True            True  \n",
       "21    114  Box Score  NaN   NaN    False        False           False  \n",
       "22    113  Box Score   OT   NaN     True         True           False  \n",
       "23     91  Box Score  NaN   NaN    False         True            True  \n",
       "24     96  Box Score  NaN   NaN     True        False            True  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建（默认）字典，存储球队上次比赛的结果。\n",
    "from collections import defaultdict\n",
    "\n",
    "#添加新列\n",
    "dataset[\"HomeLastWin\"] = False\n",
    "dataset[\"VisitorLastWin\"] = False\n",
    "\n",
    "won_last = defaultdict(int)\n",
    "#字典的键为球队，值为是否赢得上一场比赛。\n",
    "#数据集是按这种顺序排列的，如果你的数据集不是这样，你需要把代码中的dataset.iterrows()替换为dataset.sort(\"Date\").iterrows()。\n",
    "for index,row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    row[\"HomeLastWin\"] = won_last[home_team]\n",
    "    row[\"VisitorLastWin\"] = won_last[visitor_team]\n",
    "    dataset.iloc[index] = row\n",
    "    #用当前比赛（遍历到的那一行数据所表示的比赛）的结果更新两支球队上场比赛的获胜情况，以便下次再遍历到这两支球队时使用。\n",
    "    won_last[home_team] = row[\"HomeWin\"]\n",
    "    won_last[visitor_team] = not row[\"HomeWin\"]\n",
    "dataset.iloc[20:25] #输出本赛季第20~25场比赛"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "决策树是一种有监督的机器学习算法，它看起来就像是由一系列节点组成的流程图，其中位于上层节点的值决定下一步走向哪个节点。  \n",
    "创建决策树的算法有多种，大都通过迭代生成一棵树。它们从根节点开始，选取最佳特征，用于第一个决策，到达下一个节点，选择下一个最佳特征，以此类推。  \n",
    "当发现无法从增加树的层级中获得更多信息时，算法启动退出机制。  \n",
    "scikit-learn库实现了分类回归树（Classification and Regression Trees，CART）算法并将其作为生成决策树的默认算法，它支持连续型特征和类别型特征。  \n",
    "退出准则是决策树的一个重要特性。构建决策树时，最后几步决策仅依赖于少数个体，随意性大。  \n",
    "使用特定节点作出推测容易导致过拟合训练数据，而使用退出准则可以防止决策精度过高。    \n",
    "除了设定退出准则外，也可以先创建一棵完整的树，再对其进行修剪，去掉对整个过程没有提供太多信息的节点。这个过程叫作剪枝（pruning）。  \n",
    "scikit-learn库实现的决策树算法给出了退出方法，使用下面这两个选项就可以达到目的。  \n",
    " - min_samples_split：指定创建一个新节点至少需要的个体数量。  \n",
    " - min_samples_leaf：指定为了保留节点，每个节点至少应该包含的个体数量。 \n",
    " \n",
    "第一个参数控制着决策节点的创建，第二个参数决定着决策节点能否被保留。  \n",
    "决策树的另一个参数是创建决策的标准，常用的有以下两个。  \n",
    " - 基尼不纯度（Gini impurity）：用于衡量决策节点错误预测新个体类别的比例。  \n",
    " - 信息增益（Information gain）：用信息论中的熵来表示决策节点提供多少新信息。  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 58.3%\n"
     ]
    }
   ],
   "source": [
    "#scikit-learn库中导入DecisionTreeClassifier类，用它创建决策树\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.model_selection import cross_val_score\n",
    "import numpy as np\n",
    "clf = DecisionTreeClassifier()\n",
    "#用scikit-learn分类器处理。指定需要的列，使用数据框的values属性，就能获取到每支球队的上一场比赛结果。\n",
    "X_previouswins = dataset[[\"HomeLastWin\", \"VisitorLastWin\"]].values\n",
    "#近邻算法类似，决策树也是一种估计器，因此它同样有fit和predict方法。我们仍然可以用cross_val_score方法来求得交叉检验的平均正确率：\n",
    "scores = cross_val_score(clf, X_previouswins, y_true,scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Rk</th>\n",
       "      <th>Team</th>\n",
       "      <th>Overall</th>\n",
       "      <th>Home</th>\n",
       "      <th>Road</th>\n",
       "      <th>E</th>\n",
       "      <th>W</th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "      <th>SE</th>\n",
       "      <th>...</th>\n",
       "      <th>Post</th>\n",
       "      <th>≤3</th>\n",
       "      <th>≥10</th>\n",
       "      <th>Oct</th>\n",
       "      <th>Nov</th>\n",
       "      <th>Dec</th>\n",
       "      <th>Jan</th>\n",
       "      <th>Feb</th>\n",
       "      <th>Mar</th>\n",
       "      <th>Apr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>67-15</td>\n",
       "      <td>36-5</td>\n",
       "      <td>31-10</td>\n",
       "      <td>25-5</td>\n",
       "      <td>42-10</td>\n",
       "      <td>9-1</td>\n",
       "      <td>8-2</td>\n",
       "      <td>8-2</td>\n",
       "      <td>...</td>\n",
       "      <td>20-6</td>\n",
       "      <td>3-4</td>\n",
       "      <td>48-6</td>\n",
       "      <td>2-1</td>\n",
       "      <td>14-1</td>\n",
       "      <td>13-3</td>\n",
       "      <td>12-2</td>\n",
       "      <td>9-3</td>\n",
       "      <td>12-4</td>\n",
       "      <td>5-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>San Antonio Spurs</td>\n",
       "      <td>61-21</td>\n",
       "      <td>31-10</td>\n",
       "      <td>30-11</td>\n",
       "      <td>25-5</td>\n",
       "      <td>36-16</td>\n",
       "      <td>9-1</td>\n",
       "      <td>8-2</td>\n",
       "      <td>8-2</td>\n",
       "      <td>...</td>\n",
       "      <td>18-8</td>\n",
       "      <td>8-5</td>\n",
       "      <td>30-8</td>\n",
       "      <td>4-0</td>\n",
       "      <td>11-4</td>\n",
       "      <td>12-2</td>\n",
       "      <td>10-5</td>\n",
       "      <td>8-2</td>\n",
       "      <td>13-4</td>\n",
       "      <td>3-4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Houston Rockets</td>\n",
       "      <td>55-27</td>\n",
       "      <td>30-11</td>\n",
       "      <td>25-16</td>\n",
       "      <td>19-11</td>\n",
       "      <td>36-16</td>\n",
       "      <td>8-2</td>\n",
       "      <td>5-5</td>\n",
       "      <td>6-4</td>\n",
       "      <td>...</td>\n",
       "      <td>15-9</td>\n",
       "      <td>8-3</td>\n",
       "      <td>30-11</td>\n",
       "      <td>2-1</td>\n",
       "      <td>9-6</td>\n",
       "      <td>15-2</td>\n",
       "      <td>10-7</td>\n",
       "      <td>6-3</td>\n",
       "      <td>9-6</td>\n",
       "      <td>4-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>53-29</td>\n",
       "      <td>30-11</td>\n",
       "      <td>23-18</td>\n",
       "      <td>36-16</td>\n",
       "      <td>17-13</td>\n",
       "      <td>11-5</td>\n",
       "      <td>11-7</td>\n",
       "      <td>14-4</td>\n",
       "      <td>...</td>\n",
       "      <td>16-9</td>\n",
       "      <td>7-6</td>\n",
       "      <td>17-11</td>\n",
       "      <td>2-1</td>\n",
       "      <td>8-7</td>\n",
       "      <td>10-6</td>\n",
       "      <td>10-4</td>\n",
       "      <td>8-4</td>\n",
       "      <td>11-5</td>\n",
       "      <td>4-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>51-31</td>\n",
       "      <td>31-10</td>\n",
       "      <td>20-21</td>\n",
       "      <td>35-17</td>\n",
       "      <td>16-14</td>\n",
       "      <td>16-2</td>\n",
       "      <td>8-8</td>\n",
       "      <td>11-7</td>\n",
       "      <td>...</td>\n",
       "      <td>12-15</td>\n",
       "      <td>3-4</td>\n",
       "      <td>25-15</td>\n",
       "      <td>3-0</td>\n",
       "      <td>10-3</td>\n",
       "      <td>12-4</td>\n",
       "      <td>7-8</td>\n",
       "      <td>9-2</td>\n",
       "      <td>7-10</td>\n",
       "      <td>3-4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>Los Angeles Clippers</td>\n",
       "      <td>51-31</td>\n",
       "      <td>29-12</td>\n",
       "      <td>22-19</td>\n",
       "      <td>20-10</td>\n",
       "      <td>31-21</td>\n",
       "      <td>6-4</td>\n",
       "      <td>5-5</td>\n",
       "      <td>9-1</td>\n",
       "      <td>...</td>\n",
       "      <td>16-10</td>\n",
       "      <td>3-6</td>\n",
       "      <td>33-14</td>\n",
       "      <td>3-0</td>\n",
       "      <td>11-5</td>\n",
       "      <td>8-9</td>\n",
       "      <td>8-4</td>\n",
       "      <td>6-5</td>\n",
       "      <td>10-8</td>\n",
       "      <td>5-0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>Toronto Raptors</td>\n",
       "      <td>51-31</td>\n",
       "      <td>28-13</td>\n",
       "      <td>23-18</td>\n",
       "      <td>34-18</td>\n",
       "      <td>17-13</td>\n",
       "      <td>14-2</td>\n",
       "      <td>10-8</td>\n",
       "      <td>10-8</td>\n",
       "      <td>...</td>\n",
       "      <td>18-7</td>\n",
       "      <td>8-7</td>\n",
       "      <td>26-8</td>\n",
       "      <td>2-1</td>\n",
       "      <td>10-5</td>\n",
       "      <td>10-4</td>\n",
       "      <td>8-9</td>\n",
       "      <td>6-5</td>\n",
       "      <td>10-6</td>\n",
       "      <td>5-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>Utah Jazz</td>\n",
       "      <td>51-31</td>\n",
       "      <td>29-12</td>\n",
       "      <td>22-19</td>\n",
       "      <td>20-10</td>\n",
       "      <td>31-21</td>\n",
       "      <td>6-4</td>\n",
       "      <td>6-4</td>\n",
       "      <td>8-2</td>\n",
       "      <td>...</td>\n",
       "      <td>16-9</td>\n",
       "      <td>4-4</td>\n",
       "      <td>23-10</td>\n",
       "      <td>1-2</td>\n",
       "      <td>10-6</td>\n",
       "      <td>10-5</td>\n",
       "      <td>9-6</td>\n",
       "      <td>7-4</td>\n",
       "      <td>10-6</td>\n",
       "      <td>4-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>Washington Wizards</td>\n",
       "      <td>49-33</td>\n",
       "      <td>30-11</td>\n",
       "      <td>19-22</td>\n",
       "      <td>32-20</td>\n",
       "      <td>17-13</td>\n",
       "      <td>12-6</td>\n",
       "      <td>12-6</td>\n",
       "      <td>8-8</td>\n",
       "      <td>...</td>\n",
       "      <td>15-12</td>\n",
       "      <td>9-6</td>\n",
       "      <td>18-12</td>\n",
       "      <td>0-2</td>\n",
       "      <td>6-9</td>\n",
       "      <td>10-5</td>\n",
       "      <td>12-4</td>\n",
       "      <td>7-3</td>\n",
       "      <td>11-7</td>\n",
       "      <td>3-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>Oklahoma City Thunder</td>\n",
       "      <td>47-35</td>\n",
       "      <td>28-13</td>\n",
       "      <td>19-22</td>\n",
       "      <td>18-12</td>\n",
       "      <td>29-23</td>\n",
       "      <td>9-1</td>\n",
       "      <td>4-6</td>\n",
       "      <td>5-5</td>\n",
       "      <td>...</td>\n",
       "      <td>15-10</td>\n",
       "      <td>12-7</td>\n",
       "      <td>23-21</td>\n",
       "      <td>3-0</td>\n",
       "      <td>9-8</td>\n",
       "      <td>9-5</td>\n",
       "      <td>7-8</td>\n",
       "      <td>7-4</td>\n",
       "      <td>8-7</td>\n",
       "      <td>4-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>Atlanta Hawks</td>\n",
       "      <td>43-39</td>\n",
       "      <td>23-18</td>\n",
       "      <td>20-21</td>\n",
       "      <td>30-22</td>\n",
       "      <td>13-17</td>\n",
       "      <td>13-5</td>\n",
       "      <td>11-7</td>\n",
       "      <td>6-10</td>\n",
       "      <td>...</td>\n",
       "      <td>11-15</td>\n",
       "      <td>7-8</td>\n",
       "      <td>17-21</td>\n",
       "      <td>3-0</td>\n",
       "      <td>7-9</td>\n",
       "      <td>7-7</td>\n",
       "      <td>11-4</td>\n",
       "      <td>5-6</td>\n",
       "      <td>6-10</td>\n",
       "      <td>4-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>Memphis Grizzlies</td>\n",
       "      <td>43-39</td>\n",
       "      <td>24-17</td>\n",
       "      <td>19-22</td>\n",
       "      <td>15-15</td>\n",
       "      <td>28-24</td>\n",
       "      <td>5-5</td>\n",
       "      <td>5-5</td>\n",
       "      <td>5-5</td>\n",
       "      <td>...</td>\n",
       "      <td>9-15</td>\n",
       "      <td>7-5</td>\n",
       "      <td>17-20</td>\n",
       "      <td>2-1</td>\n",
       "      <td>9-7</td>\n",
       "      <td>11-6</td>\n",
       "      <td>7-7</td>\n",
       "      <td>7-4</td>\n",
       "      <td>6-9</td>\n",
       "      <td>1-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>42-40</td>\n",
       "      <td>29-12</td>\n",
       "      <td>13-28</td>\n",
       "      <td>26-26</td>\n",
       "      <td>16-14</td>\n",
       "      <td>8-10</td>\n",
       "      <td>8-8</td>\n",
       "      <td>10-8</td>\n",
       "      <td>...</td>\n",
       "      <td>13-12</td>\n",
       "      <td>4-2</td>\n",
       "      <td>23-19</td>\n",
       "      <td>1-2</td>\n",
       "      <td>8-8</td>\n",
       "      <td>7-8</td>\n",
       "      <td>9-4</td>\n",
       "      <td>6-7</td>\n",
       "      <td>6-10</td>\n",
       "      <td>5-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14</td>\n",
       "      <td>Milwaukee Bucks</td>\n",
       "      <td>42-40</td>\n",
       "      <td>23-18</td>\n",
       "      <td>19-22</td>\n",
       "      <td>27-25</td>\n",
       "      <td>15-15</td>\n",
       "      <td>10-8</td>\n",
       "      <td>10-6</td>\n",
       "      <td>7-11</td>\n",
       "      <td>...</td>\n",
       "      <td>17-10</td>\n",
       "      <td>8-2</td>\n",
       "      <td>21-22</td>\n",
       "      <td>1-2</td>\n",
       "      <td>7-6</td>\n",
       "      <td>8-8</td>\n",
       "      <td>5-10</td>\n",
       "      <td>5-6</td>\n",
       "      <td>14-4</td>\n",
       "      <td>2-4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15</td>\n",
       "      <td>Chicago Bulls</td>\n",
       "      <td>41-41</td>\n",
       "      <td>25-16</td>\n",
       "      <td>16-25</td>\n",
       "      <td>28-24</td>\n",
       "      <td>13-17</td>\n",
       "      <td>10-8</td>\n",
       "      <td>9-7</td>\n",
       "      <td>9-9</td>\n",
       "      <td>...</td>\n",
       "      <td>13-12</td>\n",
       "      <td>5-6</td>\n",
       "      <td>17-23</td>\n",
       "      <td>3-0</td>\n",
       "      <td>7-7</td>\n",
       "      <td>6-11</td>\n",
       "      <td>8-7</td>\n",
       "      <td>6-5</td>\n",
       "      <td>6-9</td>\n",
       "      <td>5-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>41-41</td>\n",
       "      <td>25-16</td>\n",
       "      <td>16-25</td>\n",
       "      <td>13-17</td>\n",
       "      <td>28-24</td>\n",
       "      <td>5-5</td>\n",
       "      <td>3-7</td>\n",
       "      <td>5-5</td>\n",
       "      <td>...</td>\n",
       "      <td>18-8</td>\n",
       "      <td>7-11</td>\n",
       "      <td>18-16</td>\n",
       "      <td>2-1</td>\n",
       "      <td>8-9</td>\n",
       "      <td>4-11</td>\n",
       "      <td>8-7</td>\n",
       "      <td>2-7</td>\n",
       "      <td>13-3</td>\n",
       "      <td>4-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17</td>\n",
       "      <td>Miami Heat</td>\n",
       "      <td>41-41</td>\n",
       "      <td>23-18</td>\n",
       "      <td>18-23</td>\n",
       "      <td>27-25</td>\n",
       "      <td>14-16</td>\n",
       "      <td>7-11</td>\n",
       "      <td>11-7</td>\n",
       "      <td>9-7</td>\n",
       "      <td>...</td>\n",
       "      <td>16-9</td>\n",
       "      <td>8-6</td>\n",
       "      <td>19-13</td>\n",
       "      <td>1-2</td>\n",
       "      <td>5-10</td>\n",
       "      <td>4-12</td>\n",
       "      <td>9-6</td>\n",
       "      <td>8-3</td>\n",
       "      <td>10-6</td>\n",
       "      <td>4-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18</td>\n",
       "      <td>Denver Nuggets</td>\n",
       "      <td>40-42</td>\n",
       "      <td>22-19</td>\n",
       "      <td>18-23</td>\n",
       "      <td>16-14</td>\n",
       "      <td>24-28</td>\n",
       "      <td>6-4</td>\n",
       "      <td>7-3</td>\n",
       "      <td>3-7</td>\n",
       "      <td>...</td>\n",
       "      <td>15-11</td>\n",
       "      <td>5-10</td>\n",
       "      <td>24-19</td>\n",
       "      <td>1-2</td>\n",
       "      <td>6-9</td>\n",
       "      <td>7-8</td>\n",
       "      <td>7-7</td>\n",
       "      <td>6-7</td>\n",
       "      <td>8-7</td>\n",
       "      <td>5-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19</td>\n",
       "      <td>Detroit Pistons</td>\n",
       "      <td>37-45</td>\n",
       "      <td>24-17</td>\n",
       "      <td>13-28</td>\n",
       "      <td>21-31</td>\n",
       "      <td>16-14</td>\n",
       "      <td>7-11</td>\n",
       "      <td>5-11</td>\n",
       "      <td>9-9</td>\n",
       "      <td>...</td>\n",
       "      <td>10-15</td>\n",
       "      <td>5-7</td>\n",
       "      <td>23-26</td>\n",
       "      <td>2-1</td>\n",
       "      <td>8-9</td>\n",
       "      <td>5-10</td>\n",
       "      <td>6-7</td>\n",
       "      <td>8-4</td>\n",
       "      <td>6-11</td>\n",
       "      <td>2-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>Charlotte Hornets</td>\n",
       "      <td>36-46</td>\n",
       "      <td>22-19</td>\n",
       "      <td>14-27</td>\n",
       "      <td>22-30</td>\n",
       "      <td>14-16</td>\n",
       "      <td>7-11</td>\n",
       "      <td>5-13</td>\n",
       "      <td>10-6</td>\n",
       "      <td>...</td>\n",
       "      <td>12-14</td>\n",
       "      <td>0-9</td>\n",
       "      <td>19-18</td>\n",
       "      <td>2-1</td>\n",
       "      <td>8-7</td>\n",
       "      <td>9-7</td>\n",
       "      <td>4-11</td>\n",
       "      <td>3-8</td>\n",
       "      <td>9-7</td>\n",
       "      <td>1-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>21</td>\n",
       "      <td>New Orleans Pelicans</td>\n",
       "      <td>34-48</td>\n",
       "      <td>21-20</td>\n",
       "      <td>13-28</td>\n",
       "      <td>14-16</td>\n",
       "      <td>20-32</td>\n",
       "      <td>5-5</td>\n",
       "      <td>4-6</td>\n",
       "      <td>5-5</td>\n",
       "      <td>...</td>\n",
       "      <td>11-14</td>\n",
       "      <td>7-6</td>\n",
       "      <td>17-22</td>\n",
       "      <td>0-3</td>\n",
       "      <td>7-9</td>\n",
       "      <td>7-9</td>\n",
       "      <td>5-9</td>\n",
       "      <td>4-7</td>\n",
       "      <td>10-6</td>\n",
       "      <td>1-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>22</td>\n",
       "      <td>Dallas Mavericks</td>\n",
       "      <td>33-49</td>\n",
       "      <td>21-20</td>\n",
       "      <td>12-29</td>\n",
       "      <td>14-16</td>\n",
       "      <td>19-33</td>\n",
       "      <td>4-6</td>\n",
       "      <td>6-4</td>\n",
       "      <td>4-6</td>\n",
       "      <td>...</td>\n",
       "      <td>11-15</td>\n",
       "      <td>4-6</td>\n",
       "      <td>14-26</td>\n",
       "      <td>0-3</td>\n",
       "      <td>3-11</td>\n",
       "      <td>7-10</td>\n",
       "      <td>8-6</td>\n",
       "      <td>6-5</td>\n",
       "      <td>7-9</td>\n",
       "      <td>2-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>23</td>\n",
       "      <td>Sacramento Kings</td>\n",
       "      <td>32-50</td>\n",
       "      <td>17-24</td>\n",
       "      <td>15-26</td>\n",
       "      <td>11-19</td>\n",
       "      <td>21-31</td>\n",
       "      <td>5-5</td>\n",
       "      <td>3-7</td>\n",
       "      <td>3-7</td>\n",
       "      <td>...</td>\n",
       "      <td>8-17</td>\n",
       "      <td>10-6</td>\n",
       "      <td>9-27</td>\n",
       "      <td>2-2</td>\n",
       "      <td>5-9</td>\n",
       "      <td>7-8</td>\n",
       "      <td>5-11</td>\n",
       "      <td>6-5</td>\n",
       "      <td>4-12</td>\n",
       "      <td>3-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>Minnesota Timberwolves</td>\n",
       "      <td>31-51</td>\n",
       "      <td>20-21</td>\n",
       "      <td>11-30</td>\n",
       "      <td>13-17</td>\n",
       "      <td>18-34</td>\n",
       "      <td>3-7</td>\n",
       "      <td>4-6</td>\n",
       "      <td>6-4</td>\n",
       "      <td>...</td>\n",
       "      <td>9-16</td>\n",
       "      <td>7-10</td>\n",
       "      <td>18-19</td>\n",
       "      <td>0-2</td>\n",
       "      <td>5-11</td>\n",
       "      <td>6-9</td>\n",
       "      <td>8-7</td>\n",
       "      <td>5-7</td>\n",
       "      <td>6-8</td>\n",
       "      <td>1-7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>25</td>\n",
       "      <td>New York Knicks</td>\n",
       "      <td>31-51</td>\n",
       "      <td>19-22</td>\n",
       "      <td>12-29</td>\n",
       "      <td>22-30</td>\n",
       "      <td>9-21</td>\n",
       "      <td>5-11</td>\n",
       "      <td>9-9</td>\n",
       "      <td>8-10</td>\n",
       "      <td>...</td>\n",
       "      <td>8-17</td>\n",
       "      <td>7-10</td>\n",
       "      <td>10-25</td>\n",
       "      <td>1-1</td>\n",
       "      <td>8-8</td>\n",
       "      <td>7-8</td>\n",
       "      <td>5-12</td>\n",
       "      <td>3-7</td>\n",
       "      <td>5-11</td>\n",
       "      <td>2-4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>26</td>\n",
       "      <td>Orlando Magic</td>\n",
       "      <td>29-53</td>\n",
       "      <td>16-25</td>\n",
       "      <td>13-28</td>\n",
       "      <td>20-32</td>\n",
       "      <td>9-21</td>\n",
       "      <td>8-10</td>\n",
       "      <td>5-13</td>\n",
       "      <td>7-9</td>\n",
       "      <td>...</td>\n",
       "      <td>8-16</td>\n",
       "      <td>5-5</td>\n",
       "      <td>10-32</td>\n",
       "      <td>0-3</td>\n",
       "      <td>7-8</td>\n",
       "      <td>8-8</td>\n",
       "      <td>4-12</td>\n",
       "      <td>3-7</td>\n",
       "      <td>5-11</td>\n",
       "      <td>2-4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27</td>\n",
       "      <td>Philadelphia 76ers</td>\n",
       "      <td>28-54</td>\n",
       "      <td>17-24</td>\n",
       "      <td>11-30</td>\n",
       "      <td>19-33</td>\n",
       "      <td>9-21</td>\n",
       "      <td>7-9</td>\n",
       "      <td>5-13</td>\n",
       "      <td>7-11</td>\n",
       "      <td>...</td>\n",
       "      <td>7-19</td>\n",
       "      <td>9-8</td>\n",
       "      <td>7-29</td>\n",
       "      <td>0-2</td>\n",
       "      <td>4-12</td>\n",
       "      <td>4-10</td>\n",
       "      <td>10-5</td>\n",
       "      <td>4-8</td>\n",
       "      <td>6-11</td>\n",
       "      <td>0-6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>28</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>26-56</td>\n",
       "      <td>17-24</td>\n",
       "      <td>9-32</td>\n",
       "      <td>10-20</td>\n",
       "      <td>16-36</td>\n",
       "      <td>3-7</td>\n",
       "      <td>3-7</td>\n",
       "      <td>4-6</td>\n",
       "      <td>...</td>\n",
       "      <td>7-17</td>\n",
       "      <td>2-4</td>\n",
       "      <td>14-31</td>\n",
       "      <td>1-2</td>\n",
       "      <td>9-8</td>\n",
       "      <td>2-14</td>\n",
       "      <td>5-10</td>\n",
       "      <td>2-8</td>\n",
       "      <td>2-12</td>\n",
       "      <td>5-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>29</td>\n",
       "      <td>Phoenix Suns</td>\n",
       "      <td>24-58</td>\n",
       "      <td>15-26</td>\n",
       "      <td>9-32</td>\n",
       "      <td>13-17</td>\n",
       "      <td>11-41</td>\n",
       "      <td>6-4</td>\n",
       "      <td>3-7</td>\n",
       "      <td>4-6</td>\n",
       "      <td>...</td>\n",
       "      <td>6-19</td>\n",
       "      <td>9-5</td>\n",
       "      <td>7-32</td>\n",
       "      <td>0-4</td>\n",
       "      <td>6-9</td>\n",
       "      <td>4-11</td>\n",
       "      <td>5-9</td>\n",
       "      <td>3-9</td>\n",
       "      <td>4-12</td>\n",
       "      <td>2-4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>30</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>20-62</td>\n",
       "      <td>13-28</td>\n",
       "      <td>7-34</td>\n",
       "      <td>11-41</td>\n",
       "      <td>9-21</td>\n",
       "      <td>3-13</td>\n",
       "      <td>4-14</td>\n",
       "      <td>4-14</td>\n",
       "      <td>...</td>\n",
       "      <td>11-15</td>\n",
       "      <td>3-6</td>\n",
       "      <td>9-30</td>\n",
       "      <td>1-3</td>\n",
       "      <td>4-9</td>\n",
       "      <td>3-12</td>\n",
       "      <td>1-15</td>\n",
       "      <td>0-10</td>\n",
       "      <td>7-10</td>\n",
       "      <td>4-3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Rk                    Team Overall   Home   Road      E      W     A  \\\n",
       "0    1   Golden State Warriors   67-15   36-5  31-10   25-5  42-10   9-1   \n",
       "1    2       San Antonio Spurs   61-21  31-10  30-11   25-5  36-16   9-1   \n",
       "2    3         Houston Rockets   55-27  30-11  25-16  19-11  36-16   8-2   \n",
       "3    4          Boston Celtics   53-29  30-11  23-18  36-16  17-13  11-5   \n",
       "4    5     Cleveland Cavaliers   51-31  31-10  20-21  35-17  16-14  16-2   \n",
       "5    6    Los Angeles Clippers   51-31  29-12  22-19  20-10  31-21   6-4   \n",
       "6    7         Toronto Raptors   51-31  28-13  23-18  34-18  17-13  14-2   \n",
       "7    8               Utah Jazz   51-31  29-12  22-19  20-10  31-21   6-4   \n",
       "8    9      Washington Wizards   49-33  30-11  19-22  32-20  17-13  12-6   \n",
       "9   10   Oklahoma City Thunder   47-35  28-13  19-22  18-12  29-23   9-1   \n",
       "10  11           Atlanta Hawks   43-39  23-18  20-21  30-22  13-17  13-5   \n",
       "11  12       Memphis Grizzlies   43-39  24-17  19-22  15-15  28-24   5-5   \n",
       "12  13          Indiana Pacers   42-40  29-12  13-28  26-26  16-14  8-10   \n",
       "13  14         Milwaukee Bucks   42-40  23-18  19-22  27-25  15-15  10-8   \n",
       "14  15           Chicago Bulls   41-41  25-16  16-25  28-24  13-17  10-8   \n",
       "15  16  Portland Trail Blazers   41-41  25-16  16-25  13-17  28-24   5-5   \n",
       "16  17              Miami Heat   41-41  23-18  18-23  27-25  14-16  7-11   \n",
       "17  18          Denver Nuggets   40-42  22-19  18-23  16-14  24-28   6-4   \n",
       "18  19         Detroit Pistons   37-45  24-17  13-28  21-31  16-14  7-11   \n",
       "19  20       Charlotte Hornets   36-46  22-19  14-27  22-30  14-16  7-11   \n",
       "20  21    New Orleans Pelicans   34-48  21-20  13-28  14-16  20-32   5-5   \n",
       "21  22        Dallas Mavericks   33-49  21-20  12-29  14-16  19-33   4-6   \n",
       "22  23        Sacramento Kings   32-50  17-24  15-26  11-19  21-31   5-5   \n",
       "23  24  Minnesota Timberwolves   31-51  20-21  11-30  13-17  18-34   3-7   \n",
       "24  25         New York Knicks   31-51  19-22  12-29  22-30   9-21  5-11   \n",
       "25  26           Orlando Magic   29-53  16-25  13-28  20-32   9-21  8-10   \n",
       "26  27      Philadelphia 76ers   28-54  17-24  11-30  19-33   9-21   7-9   \n",
       "27  28      Los Angeles Lakers   26-56  17-24   9-32  10-20  16-36   3-7   \n",
       "28  29            Phoenix Suns   24-58  15-26   9-32  13-17  11-41   6-4   \n",
       "29  30           Brooklyn Nets   20-62  13-28   7-34  11-41   9-21  3-13   \n",
       "\n",
       "       C    SE ...    Post    ≤3    ≥10  Oct   Nov   Dec   Jan   Feb   Mar  \\\n",
       "0    8-2   8-2 ...    20-6   3-4   48-6  2-1  14-1  13-3  12-2   9-3  12-4   \n",
       "1    8-2   8-2 ...    18-8   8-5   30-8  4-0  11-4  12-2  10-5   8-2  13-4   \n",
       "2    5-5   6-4 ...    15-9   8-3  30-11  2-1   9-6  15-2  10-7   6-3   9-6   \n",
       "3   11-7  14-4 ...    16-9   7-6  17-11  2-1   8-7  10-6  10-4   8-4  11-5   \n",
       "4    8-8  11-7 ...   12-15   3-4  25-15  3-0  10-3  12-4   7-8   9-2  7-10   \n",
       "5    5-5   9-1 ...   16-10   3-6  33-14  3-0  11-5   8-9   8-4   6-5  10-8   \n",
       "6   10-8  10-8 ...    18-7   8-7   26-8  2-1  10-5  10-4   8-9   6-5  10-6   \n",
       "7    6-4   8-2 ...    16-9   4-4  23-10  1-2  10-6  10-5   9-6   7-4  10-6   \n",
       "8   12-6   8-8 ...   15-12   9-6  18-12  0-2   6-9  10-5  12-4   7-3  11-7   \n",
       "9    4-6   5-5 ...   15-10  12-7  23-21  3-0   9-8   9-5   7-8   7-4   8-7   \n",
       "10  11-7  6-10 ...   11-15   7-8  17-21  3-0   7-9   7-7  11-4   5-6  6-10   \n",
       "11   5-5   5-5 ...    9-15   7-5  17-20  2-1   9-7  11-6   7-7   7-4   6-9   \n",
       "12   8-8  10-8 ...   13-12   4-2  23-19  1-2   8-8   7-8   9-4   6-7  6-10   \n",
       "13  10-6  7-11 ...   17-10   8-2  21-22  1-2   7-6   8-8  5-10   5-6  14-4   \n",
       "14   9-7   9-9 ...   13-12   5-6  17-23  3-0   7-7  6-11   8-7   6-5   6-9   \n",
       "15   3-7   5-5 ...    18-8  7-11  18-16  2-1   8-9  4-11   8-7   2-7  13-3   \n",
       "16  11-7   9-7 ...    16-9   8-6  19-13  1-2  5-10  4-12   9-6   8-3  10-6   \n",
       "17   7-3   3-7 ...   15-11  5-10  24-19  1-2   6-9   7-8   7-7   6-7   8-7   \n",
       "18  5-11   9-9 ...   10-15   5-7  23-26  2-1   8-9  5-10   6-7   8-4  6-11   \n",
       "19  5-13  10-6 ...   12-14   0-9  19-18  2-1   8-7   9-7  4-11   3-8   9-7   \n",
       "20   4-6   5-5 ...   11-14   7-6  17-22  0-3   7-9   7-9   5-9   4-7  10-6   \n",
       "21   6-4   4-6 ...   11-15   4-6  14-26  0-3  3-11  7-10   8-6   6-5   7-9   \n",
       "22   3-7   3-7 ...    8-17  10-6   9-27  2-2   5-9   7-8  5-11   6-5  4-12   \n",
       "23   4-6   6-4 ...    9-16  7-10  18-19  0-2  5-11   6-9   8-7   5-7   6-8   \n",
       "24   9-9  8-10 ...    8-17  7-10  10-25  1-1   8-8   7-8  5-12   3-7  5-11   \n",
       "25  5-13   7-9 ...    8-16   5-5  10-32  0-3   7-8   8-8  4-12   3-7  5-11   \n",
       "26  5-13  7-11 ...    7-19   9-8   7-29  0-2  4-12  4-10  10-5   4-8  6-11   \n",
       "27   3-7   4-6 ...    7-17   2-4  14-31  1-2   9-8  2-14  5-10   2-8  2-12   \n",
       "28   3-7   4-6 ...    6-19   9-5   7-32  0-4   6-9  4-11   5-9   3-9  4-12   \n",
       "29  4-14  4-14 ...   11-15   3-6   9-30  1-3   4-9  3-12  1-15  0-10  7-10   \n",
       "\n",
       "    Apr  \n",
       "0   5-1  \n",
       "1   3-4  \n",
       "2   4-2  \n",
       "3   4-2  \n",
       "4   3-4  \n",
       "5   5-0  \n",
       "6   5-1  \n",
       "7   4-2  \n",
       "8   3-3  \n",
       "9   4-3  \n",
       "10  4-3  \n",
       "11  1-5  \n",
       "12  5-1  \n",
       "13  2-4  \n",
       "14  5-2  \n",
       "15  4-3  \n",
       "16  4-2  \n",
       "17  5-2  \n",
       "18  2-3  \n",
       "19  1-5  \n",
       "20  1-5  \n",
       "21  2-5  \n",
       "22  3-3  \n",
       "23  1-7  \n",
       "24  2-4  \n",
       "25  2-4  \n",
       "26  0-6  \n",
       "27  5-2  \n",
       "28  2-4  \n",
       "29  4-3  \n",
       "\n",
       "[30 rows x 24 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入 战绩表\n",
    "standings = pd.read_csv(\"NBA_2017_expanded-standings.csv\",skiprows=[0,0])\n",
    "standings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 添加球队战绩\n",
    "dataset[\"HomeTeamRanksHigher\"] = 0\n",
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "# 如果球队改名\n",
    "#if home_team == \"New Orleans Pelicans\":\n",
    "#    home_team = \"New Orleans Hornets\"\n",
    "#elif visitor_team == \"New Orleans Pelicans\":\n",
    "#    visitor_team = \"New Orleans Hornets\"\n",
    "# 得到两支球队的排名，比较它们的排名，更新特征值。\n",
    "    home_rank = standings[standings[\"Team\"] == home_team][\"Rk\"].values[0]\n",
    "    visitor_rank = standings[standings[\"Team\"] == visitor_team][\"Rk\"].values[0]\n",
    "    row[\"HomeTeamRanksHigher\"] = int(home_rank > visitor_rank)\n",
    "    dataset.iloc[index] = row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 65.2%\n"
     ]
    }
   ],
   "source": [
    "#用cross_val_score函数测试结果\n",
    "X_homehigher = dataset[[\"HomeLastWin\", \"VisitorLastWin\",\"HomeTeamRanksHigher\"]].values\n",
    "clf = DecisionTreeClassifier()\n",
    "scores = cross_val_score(clf, X_homehigher, y_true,scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#我们来统计两支球队上场比赛的情况，作为另一个特征。增强 预测结果\n",
    "#创建字典，保存上场比赛的获胜队伍，在数据框中建立新特征\n",
    "last_match_winner = defaultdict(int)\n",
    "dataset[\"HomeTeamWonLast\"] = 0\n",
    "\n",
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    teams = tuple(sorted([home_team, visitor_team]))\n",
    "    #查找字典，找到两支球队上次比赛的赢家。然后，更新数据框中这条数据。\n",
    "    row[\"HomeTeamWonLast\"] = 1 if last_match_winner[teams] == row[\"Home Team\"] else 0 \n",
    "    dataset.iloc[index] = row\n",
    "    # 更新last_match_winner字典，值为两支球队在当前场次比赛中的胜出者，两支球队再相逢时可将其作为参考。\n",
    "    winner = row[\"Home Team\"] if row[\"HomeWin\"] else row[\"Visitor Team\"]\n",
    "    last_match_winner[teams] = winner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 65.2%\n"
     ]
    }
   ],
   "source": [
    "# 测试预测结果\n",
    "X_lastwinner = dataset[[\"HomeTeamRanksHigher\", \"HomeTeamWonLast\"]].values\n",
    "clf = DecisionTreeClassifier()\n",
    "scores = cross_val_score(clf, X_lastwinner, y_true,scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "虽然决策树能够处理特征值为类别型的数据，但scikit-learn库所实现的决策树算法要求先对这类特征进行处理。用LabelEncoder转换器就能把字符串类型的球队名转化为整型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "encoding = LabelEncoder()\n",
    "#将主场球队名称转化为整型：\n",
    "encoding.fit(dataset[\"Home Team\"].values)\n",
    "encoding.fit(dataset[\"Visitor Team\"].values)\n",
    "#抽取所有比赛的主客场球队的球队名（已转化为数值型）并将其组合（在NumPy中叫作“stacking”，是向量组合的意思）起来，形成一个矩阵\n",
    "home_teams = encoding.transform(dataset[\"Home Team\"].values)\n",
    "visitor_teams = encoding.transform(dataset[\"Visitor Team\"].values)\n",
    "X_teams = np.vstack([home_teams, visitor_teams]).T\n",
    "#使用OneHotEncoder转换器把这些整数转换为二进制数字。每个特征用一个二进制数字①来表示。\n",
    "#例如，LabelEncoder为芝加哥公牛队分配的数值是7，那么OneHotEncoder为它分配的二进制数字的第七位就是1，其余队伍的第七位就是0。\n",
    "#每个可能的特征值都这样处理，而数据集会变得很大。\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "onehot = OneHotEncoder()\n",
    "#在相同的数据集上进行预处理和训练操作，将结果保存起来备用。\n",
    "X_teams_expanded = onehot.fit_transform(X_teams).todense()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 58.6%\n"
     ]
    }
   ],
   "source": [
    "#测试\n",
    "clf = DecisionTreeClassifier()\n",
    "scores = cross_val_score(clf, X_teams_expanded, y_true,scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 随机森林算法\n",
    "一棵决策树可以学到很复杂的规则。然而，很可能会导致过拟合问题——学到的规则只适用于训练集。  \n",
    "解决方法之一就是调整决策树算法，限制它所学到的规则的数量。例如，把决策树的深度限制在三层，只让它学习从全局角度拆分数据集的最佳规则，  \n",
    "不让它学习适用面很窄的特定规则，这些规则会将数据集进一步拆分为更加细致的群组。  \n",
    "使用这种折中方案得到的决策树泛化能力强，但整体表现稍弱。  \n",
    "为了弥补上述方法的不足，我们可以创建多棵决策树，用它们分别进行预测，再根据少数服从多数的原则从多个预测结果中选择最终预测结果。这正是随机森林的工作原理。  \n",
    "scikit-learn库中的RandomForestClassifier就是对随机森林算法的实现，它提供了一系列参数。  \n",
    "因为它使用了DecisionTreeClassifier的大量实例，所以它俩的很多参数是一致的，比如决策标准（基尼不纯度/信息增益）、max_features和min_samples_split。\n",
    "新参数:\n",
    " - n_estimators：用来指定创建决策树的数量。该值越高，所花时间越长，正确率（可能）也越高。\n",
    " - oob_score：如果设置为真，测试时将不使用训练模型时用过的数据。\n",
    " - n_jobs：采用并行计算方法训练决策树时所用到的内核数量。  \n",
    " \n",
    "scikit-learn库提供了用于并行计算的Joblib库。n_jobs指定所用的内核数。默认使用1个内核——如果CPU是多核的，可以多用几个，或者将其设置为1，开动全部马力。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 55.7%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "clf = RandomForestClassifier()\n",
    "scores = cross_val_score(clf, X_teams, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 57.8%\n"
     ]
    }
   ],
   "source": [
    "#随机森林使用不同的特征子集进行学习，应该比普通的决策树更为高效。下面来看一下多用几个特征效果如何。\n",
    "X_all = np.hstack([X_homehigher, X_teams])\n",
    "clf = RandomForestClassifier()\n",
    "scores = cross_val_score(clf, X_all, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 64.5%\n"
     ]
    }
   ],
   "source": [
    "#使用GridSearchCV类搜索最佳参数\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "parameter_space = {\n",
    "    \"max_features\": [2, 'auto'],\n",
    "    \"n_estimators\": [100,],\n",
    "    \"criterion\": [\"gini\", \"entropy\"],\n",
    "    \"min_samples_leaf\": [2, 4, 6],\n",
    "    \"max_depth\": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n",
    "}\n",
    "clf = RandomForestClassifier()\n",
    "grid = GridSearchCV(clf, parameter_space)\n",
    "grid.fit(X_all, y_true)\n",
    "print(\"Accuracy: {0:.1f}%\".format(grid.best_score_ * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='entropy',\n",
      "            max_depth=3, max_features='auto', max_leaf_nodes=None,\n",
      "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
      "            min_samples_leaf=2, min_samples_split=2,\n",
      "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1,\n",
      "            oob_score=False, random_state=None, verbose=0,\n",
      "            warm_start=False)\n"
     ]
    }
   ],
   "source": [
    "#输出用网格搜索找到的最佳模型，查看都使用了哪些参数。代码如下：\n",
    "print(grid.best_estimator_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 用pandas提供的函数创建特征。\n",
    "```Python\n",
    "dataset[\"New Feature\"] = feature_creator()\n",
    "```  \n",
    "`feature_creator`函数返回数据集中每条数据的各个特征值。常用数据集作为参数。  \n",
    "`dataset[\"New Feature\"] = feature_creator(dataset)`最直接的做法是一开始为新特征设置默认的值，比如0。如下所示：  \n",
    "`dataset[\"My New Feature\"] = 0`  \n",
    "接下来，遍历数据集，计算所需特征。本章曾多次用下面这种形式创建新特征。\n",
    "\n",
    "```python\n",
    "for index, row in dataset.iterrows():\n",
    "home_team = row[\"Home Team\"]\n",
    "visitor_team = row[\"Visitor Team\"]\n",
    "# Some calculation here to alter row\n",
    "dataset.ix[index] = row\n",
    "```  \n",
    "请注意，上面这种遍历方法效率不高。如果你要用的话，请一次性处理所有特征。常用的“最佳做法”就是每条数据最好只处理一次。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
